package org.NooLab.glue.msgframe;

import java.io.Serializable;
import java.util.SortedMap;
import java.util.Vector;

import org.NooLab.glue.MessageBoard;
import org.NooLab.glue.storage.MsgBoardDataStorage;
import org.NooLab.utilities.files.DFutils;




public class Notifications implements Serializable{

	transient private static final long serialVersionUID = 5289931238112852368L;
	// =================================

	// object references ..............
	
	transient MessageBoard messageBoard ;
	transient MsgBoardDataStorage storage;
	
	
	// main variables / properties ....

	protected Vector<Notification> notes = new Vector<Notification>();
	
	String filename;
	
	// constants ......................
	
	

	// volatile variables .............

	
	
	// helper objects .................
	 
	transient DFutils fileutil = new DFutils();
	
	
	// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
	public Notifications( MessageBoard mb){
		messageBoard = mb ;
		
		storage = messageBoard.getMsgBoardStorage() ; 
		
		createFilename( "msb", serialVersionUID) ;
		load();
	}
	
	// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
 
	
	/**
	 * for persistence of MessageBoard, the Notifications have to be stored
	 */
	public void save(){
		

		try{
			
			storage.storeTransactionDataObj( notes, filename) ;
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	public void load(){
		Object obj;
		
		
		try{
			if (fileutil.fileexists(filename)==false){
				
				return;
			}
			
			obj = storage.readTransactionDataObj(filename) ;
			notes  = (Vector<Notification> )obj ;
			
		}catch(Exception e){
			
		}
	}
	
	protected String createFilename( String name, long serialUID){
		String path, filname = "";
		
		path = storage.getHibernationPath("board") ;
		 
		filname = "~noty-"+name+"-"+serialUID+".lst" ;
		filname = fileutil.createPath( path, filname) ;
		
		
		filename = filname;
		
		return filname;
	}
	
	public void close( Notification  note){
		
		
	}
	
	public Vector<Notification> getNotes() {
		return notes;
	}


	public void setNotes(Vector<Notification> notes) {
		this.notes = notes;
	}




	public int size(){
		int n=0;
		
		if (notes==null){
			notes = new Vector<Notification>() ;
		}
		n = notes.size();
		
		return n;
	}
	
	public Notification createNote( long transactionID , String bufferfilename){
		Notification note = new Notification();
		
		note.filename = bufferfilename;
		note.resultsOfTransactionID = transactionID;
		
		
		return note;
	}
			
	public Notification get(int index){
		Notification note=null;
		
		if ((index>=0) && (index<notes.size())){ 
			note = notes.get(index) ;
		}
		
		return note;
	}
	
	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	synchronized public void add( Notification note){
		boolean hb=true;
		Notification n;
		
		if (notes.contains(note)==false){
			
			for (int i=0;i<notes.size();i++){
				n = notes.get(i) ;
				if (n.resultsOfTransactionID == note.resultsOfTransactionID){
					hb=false;
					break;
				}
			}
			if (hb){
				notes.add(note) ;
			}
		}
	}
	
	synchronized public void add( long transactionID , String bufferfilename){
		
		Notification note = new Notification();
		boolean hb=true;
		Notification n;
		
		
		note.filename = bufferfilename;
		note.resultsOfTransactionID = transactionID;
		
		for (int i=0;i<notes.size();i++){
			n = notes.get(i) ;
			if (n.resultsOfTransactionID == transactionID){
				hb=false;
				break;
			}
		}
		if (hb){
			notes.add(note) ;
		}
		
	}
	
	public Notification getNotificationByTransactionID( long transactionID){ 
		Notification note=null;
	
		long tid;
		
		try{
			
			for (int i=0;i<notes.size();i++){
				
				note = notes.get(i) ;
				if (note!=null){
					tid = note.getResultsOfTransactionID();

					if (tid == transactionID) {
						break;
					}
				}
			} // i->
			
		}catch(Exception e){
			// educated silence
		}
		
		return note;
	}
	/**
	 * TODO : block notifications for the given transaction ID while removing it...
	 * 
	 * @param transactionID
	 */
	synchronized public void removeNotificationByTransactionID( long transactionID){  
		
		Notification note; 
		long tid;
		
		try{
			
			
			
			for (int i=0;i<notes.size();i++){
				
				note = notes.get(i) ;
				if (note!=null){
					tid = note.getResultsOfTransactionID();

					if (tid == transactionID) {
						note = null;
						removeItem(i);
						save();
						break;
					}
				}
			} // i->
			
		}catch(Exception e){
			// educated silence
		}
		
	}
	
	synchronized public void removeItem( int index){
		if ((index>=0) && (index<notes.size())){
			notes.remove(index) ;
		}
	}

	synchronized public void remove(int index){
		if ((index>=0) && (index<notes.size())){ 
			notes.remove(index);
		}
	}
	
}
